package ModelLayer;
import java.util.ArrayList;

/**
 * Write a description of class ProductContainer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ProductContainer  //Class used to make a list containing products
{
    // instance variables - replace the example below with your own
    private ArrayList<Product> productList;
    private static ProductContainer instance;

    /**
     * Constructor for objects of class ProductContainer
     */
    public ProductContainer()
    {
        productList = new ArrayList<Product>();
    }

    public static ProductContainer getInstance()  //here is used singleton. That way, there is only made one list
    {
        if(instance == null)
        {
            instance = new ProductContainer();
        }

        return instance;
    }

    public void addProduct(Product newProduct)  //adds a product to the list
    {
        productList.add(newProduct);
    }

    public void addSerialnumber(long productId, long serialnumber)  //adds a serial to the product
    {
        int index = 0;
        boolean found = false;

        while(index < productList.size() && found == false)
        {

            String id1 = String.valueOf(productId);
            String idIndex = String.valueOf(productList.get(index).getProductId());

            if(idIndex.equals(id1))
            {
                found = true;

                productList.get(index).addSerialnumber(serialnumber);
            }
            else
            {
                index++;
            }
        }

        if(found==false)
        {
            System.out.println("You don't have a dvd with that ID to add a sample to... maybe you should make a new dvd or check the dvd-list?");
        }
    }

    public void findProduct(String productName)  //used to find a product in the list
    {
        ArrayList<Product> productFindings;
        productFindings = new ArrayList<Product>();

        int index = 0;
        boolean found = false;
        while(!found && index < productList.size())
        {

            if(productList.get(index).getProductName().contains(productName))
            {
                productFindings.add(productList.get(index));
            }
            else
            {
                index++;
            }

        }

        if(productList.size() < 0)
        {
            System.out.println("Product(s) not found");

        }
        else
        {
            for(Product p : productFindings)
            {
                p.showInfo();
            }
        }       
    }

    public PartOrder getProduct(int productId, int amount)  //used to get a product and serials in the list
     {
        int index = 0;
        boolean found = false;
        PartOrder pO;
        //Product p;
        while(!found && index < productList.size())
        {
            String pId = ""+productId;
            if(productList.get(index).getProductId().equals(pId))
            {
                found = true;
            }
            else
            {
                index++;
            }
        }
        if(!found)
        {
            System.out.println("Product not found!");
        }
        
        if(productList.get(index).serialnumberList.size() < amount)
        {
            System.out.println("We only have "+productList.get(index).serialnumberList.size()+" in stock!");
            pO = null;
        }
        else
        {
            Product p = productList.get(index);            
            ArrayList<Serialnumber> serialList;
            serialList = null;
            
            int index2 = 0;
            while(index2 < amount)
            {
                serialList.add(productList.get(index).serialnumberList.get(index2));
                productList.get(index).serialnumberList.remove(index2);
                
                index2++;
            }
            
            pO = new PartOrder(p, amount, serialList);
            System.out.println("Product(s) added to your order!");
        }
                
        return pO;
    }
}

